package mafiagame.roles;

import java.util.ArrayList;
import java.util.List;

import mafiagame.Action;
import mafiagame.Game;
import mafiagame.Mark;
import mafiagame.Mark.MarkScope;
import mafiagame.Mark.MarkType;
import mafiagame.roles.Detective.DetectiveRole;
import mafiagame.Player;
import mafiagame.Role;
import mafiagame.RoleDefinition;

public class Doctor extends RoleDefinition
{
	public DefinitionType getDefinitionType()
	{
		return DefinitionType.SAVING;
	}

	public String explainRole()
	{
		return "You are the doctor! Every night, you will have the "
				+ "chance to save a player from being killed that night. Each player can only be saved once per game, "
				+ "however, so you have to think carefully! In order to save a person, IM me with '<b>save</b> "
				+ "[player]' during the night.";
	}

	public Doctor()
	{
		super();
	}
	public String getName()
	{
		return "DOCTOR";
	}

	public void assignRole(Game g, Player p)
	{
		p.roles.add(new DoctorRole(g, p));
	}

	public static class DoctorRole extends Role
	{
		public class HealAction extends Action
		{
			@Override
			public String getDescription()
			{
				return "This will allow you to <b>save</b> a player for this night phase. Saving a player means"
						+ " that if they are targeted for a kill, they will not actually be killed. Note that some"
						+ " kills cannot be saved!";
			}

			@Override
			public String getCommand()
			{
				return "save";
			}

			@Override
			public boolean Execute(String message)
			{
				String[] tokens = message.split(" ");
				if (tokens.length != 2)
				{
					myGame.communicator
							.sendPrivateMessage(
									myPlayer.username,
									"The heal must be of the format 'save [number]' where the number is the player number you wish to save.");
					return false;
				}
				Player p = myGame.parsePlayer(tokens[1]);
				if (p == null)
				{
					myGame.communicator
							.sendPrivateMessage(myPlayer.username,
									"You must choose a player who is actually playing.");
					return false;
				}
				if (!p.living)
				{
					myGame.communicator.sendPrivateMessage(myPlayer.username,
							"You must choose a player who is alive to heal.");
					return false;
				}

				if (alreadySaved.contains(p))
				{
					myGame.communicator.sendPrivateMessage(myPlayer.username,
							"You have already healed <b>" + p.username
									+ "</b> this game!");
					return false;
				}

				Mark m = new Mark();
				m.type = MarkType.SAVE_KILL;
				m.duration = 1;
				m.scope = MarkScope.NIGHT;
				m.isBlockable = true;
				m.isRedirectable = true;
				m.source = myPlayer;
				p.marks.add(m);

				alreadySaved.add(p);

				myGame.communicator.sendPrivateMessage(myPlayer.username,
						"Healing <b>" + p.username + "</b> tonight!");

				return true;
			}

			@Override
			public boolean isPublic()
			{
				return false;
			}

		}

		private List<Player> alreadySaved;

		public DoctorRole(Game g, Player p)
		{
			super(g, p);

			alreadySaved = new ArrayList<Player>();
		}

		public String getName()
		{
			return "DOCTOR";
		}

		public String getHelp()
		{
			return "You are the doctor! Every night, you will have the "
					+ "chance to save a player from being killed that night. Each player can only be saved once per game, "
					+ "however, so you have to think carefully! In order to save a person, IM me with '<b>save</b> "
					+ "[player]' during the night.";
		}

		@Override
		public void SetupPhase()
		{

		}

		@Override
		public void NightPhase()
		{
			myGame.addAction(myPlayer.username, new HealAction(),
					false);
		}

		@Override
		public void DayPhase()
		{

		}
	}
}
